<?php
  //set session save path
  session_save_path('/home/football/public_html/sessions/');
  // Initialize a session. 
  session_start(  );
            //session_destroy();             
  $count++;                         
  $sessionId = session_id();

?>
	<head>
		<link rel="stylesheet" href="../NFLPool.css" type="text/css">
	<script language="javascript">
	function register(url) {
		window.parent.mainFrame.location = url;
	}
	</script>
	</head>
	<body bgcolor="#DDDDDD">
	<?
	
	/*
	Generate poll number for today by doing a modulus calculation on the day of
	the year (0-365) and the number of polls.
	$poll = date("z") % 3;
	$filename = "poll".$poll.".txt";
	$IPfilename = "poll".$poll."ip.txt";
	*/
		//$poll = date("z") % 3;
		$filename = "playoffs.txt";
		$IPfilename = "playoffsip.txt";
		$fl=fopen($filename,'r');
	
		$name = fgets($fl, 4096);
		$options[0] = fgets($fl, 5);
	
		$temp = $options[0];
		while ($temp) {
			$theoptions[$temp] = fgets($fl, 4096);
			$temp = $temp - 1;
		}
	
		$temp = $options[0];
		while ($temp) {
			$thevalues[$temp] = fgets($fl, 4096);
			$temp = $temp - 1;
		}
		fclose($fl);
		//Define an array of colors to show a "bar" graph of the results using a table of %(of votes) width
		$colors = array("#FF0000","#FFFF00","#FF00FF","#00FF00","#00FFFF","#0000FF");
	
		if ($results) {
			echo "$name<br>";
			$temp = $options[0];
			//get the total number of votes
		$tempnum = $options[0];
			$total = 0;
			while ($tempnum) {
			$total += Trim($thevalues[$tempnum]);
			$tempnum = $tempnum - 1;
			}
		echo "(<font color=\"#0066CC\">$total</font> votes)<br>";
		//loop through the options and display the
			$currentColor = 0;
			while ($temp) {
				if ($thevalues[$temp] == 0) {
					$percent = 0;
				} else {
			//calculate the % by dividing # of votes by toal votes, and x 100  
			$percent = 100 * Trim($thevalues[$temp]) / $total;
			//trim the percent down to 2 decimail places
					if (strlen($percent) > 4) {
					   $percent = substr($percent, 0, 4);
					}
				}//end if ($thevalues[$temp] == 0)
			//display the option and it # of votes
				echo "<B><U>$theoptions[$temp]</U>:</B>"; // (<font color=\"#0066CC\">$thevalues[$temp]</font> votes)
			//display a table of % width with a different color
			if ($percent > 0) {
				echo "<TABLE bgcolor=\"".$colors[$currentColor]."\" width=\"".$percent."%\"><TR><TD>".$percent."%</TD></TR></TABLE>";
			} else {
			echo "<BR>".$percent."%<br>";
			} //end if ($percent > 0)
				$temp = $temp - 1;
			$currentColor = $currentColor + 1;
			}//end WHILE loop
		echo "<BR><a href=\"vote.php\">Back To Poll</a>";
	   } elseif ($vote) {
	   		if ($userID) { 
				//Open $IPfilename and get a list of all IP addresses used to vote
				$file_handle = fopen($IPfilename, "r"); 
				$temp = fread ($file_handle, filesize ($IPfilename)); 
				fclose ($file_handle); 
				$IPs = explode ("\n", $temp);
			
				//search the list for the current user IP
				$voted = False;
				foreach($IPs as $value)
				{
					if ($value == $userID) {	   
					$voted = True;
					}//end if
				}//end foreach loop
		
				if ($voted != True) { //If the user has not voted before
					//display the voted optin and increase the number of votes that option has recieved
					//write IP address to file so they cannot vote again.
					if (!$file_handle = fopen($IPfilename,"a")) { echo "Cannot open file"; }   
					if (!fwrite($file_handle, $userID."\n")) { echo "Cannot write to file"; }       
					//close($file_handle);   
						//display the voted option
					echo "You voted for <H4>$theoptions[$optradio]</H4><br>";
					//
					unlink($filename);
					$fl = fopen($filename, 'w');
					fputs($fl, "$name");
					fputs($fl, "$options[0]");
			
					$temp = $options[0];
						while ($temp) {
						   fputs($fl, "$theoptions[$temp]");
						   $temp = $temp - 1;
						}//end WHILE lop
			
						$temp = $options[0];
						while ($temp) {
						   if ($temp == $optradio) {
							   fputs($fl, "$thevalues[$temp]" + 1);
							   fputs($fl, "\n");
							   $temp = $temp - 1;
						   } else {
							   fputs($fl, "$thevalues[$temp]");
							   $temp = $temp - 1;
						   }//end if ($temp == $optradio)
						}//end WHILE loop
						fclose($fl);
				echo "<form method=\"post\" action=\"$PHP_SELF\"><input type=\"submit\" name=\"results\" value=\"See Results\"></form>";
			} else { //if $voted does EQ True (i.e. The user has voted before)
				echo "You have already submitted a vote.<form method=\"post\" action=\"$PHP_SELF\">&nbsp;<input type=\"submit\" name=\"results\" value=\"See Results\"></form>";
			}//end if ($voted != True)
		} else {
			?>
			<SCRIPT language="JavaScript">
			<!--
			  var w = 450;
			  var h = 150;
			  var winl = (screen.width-w)/2;
			  var wint = (screen.height-h)/2;
			  if (winl < 0) winl = 0;
			  if (wint < 0) wint = 0;
			  var settings = 'height=' + h + ',';
			  settings += 'width=' + w + ',';
			  settings += 'top=' + wint + ',';
			  settings += 'left=' + winl + ',';
			  settings += 'resizable=no,scrolling=no,menu=no,toolbar=no';
			  win = window.open('warning.html','warning',settings);
			  window.location="vote.php";
			//-->
			</SCRIPT>
			<?
		}//end if ($userID)
	   } else {
		   echo "$name<br>";
		   $temp = $options[0];
		   echo "<form method=\"post\" action=\"$PHP_SELF\">";
		   while ($temp) {
			   echo "<input type=\"radio\" name=\"optradio\" value=\"$temp\"> $theoptions[$temp]<br>";
			   $temp = $temp - 1;
		   }
		   echo "<input type=\"submit\" name=\"vote\" value=\"Vote\">&nbsp;";
		   echo "<input type=\"submit\" name=\"results\" value=\"See Results\">";
	   }
	echo "</body></html>"
	?> 